<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工名称" prop="empName">
        <el-input
          :style="{width: '150px'}"
          v-model="queryParams.empName"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工电话" prop="empPhone">
        <el-input
          :style="{width: '150px'}"
          v-model="queryParams.empPhone"
          placeholder="请输入员工电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工地址" prop="empAddress">
        <el-input
          :style="{width: '150px'}"
          v-model="queryParams.empAddress"
          placeholder="请输入员工地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工部门">
        <el-select v-model="queryParams.depId" placeholder="请选择员工部门">
          <el-option  label="所有部门" value=""></el-option>
          <el-option v-for="(select,index) in selectList" :label="select.depName" :value="select.depId"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['personnel:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['personnel:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['personnel:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          round
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['personnel:employee:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-refresh-right"
          size="mini"
          @click="handleRollback"
          v-hasPermi="['personnel:employee:export']"
        >回滾</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column  label="员工头像" align="center">
        <!--Vue插槽：（2.6.0以后版本弃用slot和slot-scope，改用v-slot)-->
        <template v-slot="{ row }">
          <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                    :src="getImage(row.groupName,row.fileName)">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="员工编号" align="center" prop="empId" />
      <el-table-column label="员工名称" align="center" prop="empName" />
      <el-table-column label="员工年龄" align="center" prop="empAge" />
      <el-table-column label="员工性别" align="center" prop="empSex" />
      <el-table-column label="员工电话" align="center" prop="empPhone" />
      <el-table-column label="员工地址" align="center" prop="empAddress" />
      <el-table-column label="员工部门" align="center" prop="depName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['personnel:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['personnel:employee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-show="title == '修改员工管理'" label="回显图片" align="center">
          <!--Vue插槽：（2.6.0以后版本弃用slot和slot-scope，改用v-slot)-->
          <template >
            <el-image style="width: auto; height: 90px; border:none;cursor: pointer;"
                      :src="getImage(form.groupName,form.fileName)">
            </el-image>
          </template>
        </el-form-item>
        <el-form-item label="员工头像" :label-width="formLabelWidth">
          <el-upload
            class="upload-demo"
            drag
            action="http://localhost:8080/image/uploadFastDFS/"
            :on-success="handle_success"
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="员工名称" prop="empName">
          <el-input v-model="form.empName" placeholder="请输入员工名称" />
        </el-form-item>
        <el-form-item label="员工年龄" prop="empAge">
          <el-input v-model="form.empAge" placeholder="请输入员工年龄" />
        </el-form-item>
        <el-form-item label="员工性别" prop="empSex">
<!--          <el-input v-model="form.empSex" placeholder="请输入员工性别" />-->
          <el-radio v-model="form.empSex" label="1">男</el-radio>
          <el-radio v-model="form.empSex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="员工电话" prop="empPhone">
          <el-input v-model="form.empPhone" placeholder="请输入员工电话" />
        </el-form-item>
        <el-form-item label="员工地址" prop="empAddress">
          <el-input v-model="form.empAddress" placeholder="请输入员工地址" />
        </el-form-item>
        <el-form-item label="员工部门" prop="depId">
          <el-select v-model="form.depId" placeholder="员工部门">
            <el-option v-for="(select,index) in selectList" :label="select.depName" :value="select.depId"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee,rollback,listDep } from "@/api/personnel/employee";

export default {
  name: "Employee",
  data() {
    return {
      formLabelWidth: '80px',
      // 下拉列表
      selectList:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工管理表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        empName: null,
        empSex: null,
        empPhone: null,
        empAddress: null,
        depId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        empName: [
          { required: true, message: "员工名称不能为空", trigger: "blur" }
        ],
        depId: [
          { required: true, message: "员工部门不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDepList();
  },
  methods: {
    // 图片加载到本地获取到返回的名称给到from表单数据
    handle_success(res){
        this.form.groupName = res.groupName
        this.form.fileName = res.fileName
        this.form.fileSize = res.fileSize
        this.form.fileType = res.fileType
        this.form.oldFileName = res.oldFileName
    },
    getDepList(){
      listDep().then(res=>{
        this.selectList = res.rows
      })
    },
    getImage(groupName,fileName){
      return 'http://localhost:8080/image/downloadShowFastDFS?groupName='+groupName+'&fileName='+fileName
    },
    /** 查询员工管理列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        for (let i = 0; i <this.employeeList.length ; i++) {
            if (this.employeeList[i].empSex == 1){
              this.employeeList[i].empSex = '男'
            }else {
              this.employeeList[i].empSex = '女'
            }
        }
         this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        empId: null,
        empName: null,
        empAge: null,
        empSex: null,
        empPhone: null,
        empAddress: null,
        empSrc: null,
        depId: null,
        createTime: null,
        updateTime: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.empId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const empId = row.empId || this.ids
      getEmployee(empId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.empId != null) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const empIds = row.empId || this.ids;
      this.$modal.confirm('是否确认删除员工管理编号为"' + empIds + '"的数据项？').then(function() {
        return delEmployee(empIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('personnel/employee/export', {
        ...this.queryParams
      }, `employee_${new Date().getTime()}.xlsx`)
    },
    handleRollback(){
      rollback().then((res)=>{
        if (!res){
          this.$message({
            message: '目前不存在撤销功能',
            type: 'warning'
          });
        }else {
          this.getList();
        }
      })
    }
  }
};
</script>
